package com.bocai.kelalacar.ui.car.order.frag;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;

import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseFragment;
import com.bocai.kelalacar.base.BaseRecyclerAdapter;
import com.bocai.kelalacar.bean.CarDriverBean;
import com.bocai.kelalacar.bean.MyGradOrderBean;
import com.bocai.kelalacar.bean.MyPublishStrokeBean;
import com.bocai.kelalacar.bean.OrderDetailBean;
import com.bocai.kelalacar.ui.car.main.act.MainActivity;
import com.bocai.kelalacar.ui.car.order.act.RushOrderDetailActivity;
import com.bocai.kelalacar.ui.car.order.ada.RushOrderAda;
import com.bocai.kelalacar.ui.car.order.contract.MyOrderContract;
import com.bocai.kelalacar.ui.car.order.module.MyOrderModule;
import com.bocai.kelalacar.ui.car.order.presenter.MyOrderPresenter;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by yuanfei on 2017/6/21.
 */

public class RushOrderFrag extends BaseFragment implements MyOrderContract.view,XRecyclerView.LoadingListener {
    @BindView(R.id.xrecycle)
    XRecyclerView xrecycle;
    RushOrderAda ada;
    @Inject
    MyOrderPresenter presenter;
    private int pageIndex = 1;

    @Override
    protected int getLayoutResource() {
        return R.layout.frag_rush_order;
    }

    @Override
    public void initPresenter() {
        App.get(getActivity()).getAppComponent().addSub(new MyOrderModule(this)).inject(this);
    }

    @Override
    protected void initView() {
        ada = new RushOrderAda();
        xrecycle.setLayoutManager(new LinearLayoutManager(getActivity()));
        xrecycle.setAdapter(ada);
        ada.setOnItemOnListener(new BaseRecyclerAdapter.OnItemClickedListener() {
            @Override
            public void onItemClicked(int id, int index) {
                startActivity(new Intent(getActivity(),RushOrderDetailActivity.class).putExtra("orderId",ada.getDataList().get(index).getOrderId()));
            }
        });
        xrecycle.setLoadingListener(this);
        xrecycle.refresh();
    }

    @Override
    public void myStrokePublish(MyPublishStrokeBean bean) {

    }

    @Override
    public void myGradOrder(MyGradOrderBean bean) {
        xrecycle.refreshComplete();
        xrecycle.loadMoreComplete();
        if (bean != null &&bean.getOrderList()!=null&&bean.getOrderList().size()!=0){
            if (pageIndex == 1){
                ada.clear();
                ada.addItems(bean.getOrderList());
            }else{
                ada.addItems(bean.getOrderList());
            }
            if (bean.getOrderList().size()<10){
                xrecycle.setLoadingMoreEnabled(false);
            }else{
                xrecycle.setLoadingMoreEnabled(true);
            }
        }else {
            xrecycle.setLoadingMoreEnabled(false);
        }
    }

    @Override
    public void getOrderDetail(OrderDetailBean bean) {

    }

    @Override
    public void getCarDriverList(CarDriverBean bean) {

    }

    @Override
    public void teamArrangeSuccess() {

    }

    @Override
    public void startStrokeSuccess() {

    }

    @Override
    public void cancelOrderSuccess() {

    }

    @Override
    public void finishOrderSuccess() {

    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")){
            startActivity(new Intent(getContext(),MainActivity.class).putExtra("index",1));
            getActivity().finish();
        }else if (msg.equals("网络异常")){
            xrecycle.refreshComplete();
            xrecycle.loadMoreComplete();
        }
    }

    @Override
    public void onRefresh() {
        pageIndex = 1;
        presenter.myGrabOrder(pageIndex+"","10");
    }

    @Override
    public void onLoadMore() {
        pageIndex = pageIndex + 1;
        presenter.myGrabOrder(pageIndex+"","10");
    }
}
